{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **演示0101：创建数组**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "### **案例1：直接创建数组并给定初始值**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">**分别创建下列形式的一维数组：**  \n",
    "[3 6 5 2 7]  \n",
    "[0. 0. 0. 0. 0.]  \n",
    "[1. 1. 1. 1. 1.]  \n",
    "[3. 3. 3. 3. 3.]  \n",
    ">* 使用*array*方法直接初始化数组元素  \n",
    "* 使用*zeros, ones, full*方法  \n",
    "* 注意数组中每个元素默认的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 6 5 2 7]\n",
      "[0. 0. 0. 0. 0.]\n",
      "[1. 1. 1. 1. 1.]\n",
      "[3. 3. 3. 3. 3.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a1 = np.array([3, 6, 5, 2, 7])\n",
    "size = 5\n",
    "a2 = np.zeros(size)\n",
    "a3 = np.ones(size)\n",
    "a4 = np.full(size, 3.0)\n",
    "print(a1)\n",
    "print(a2)\n",
    "print(a3)\n",
    "print(a4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **创建上一个例子中的数组，但是要求每个元素为整数类型**  \n",
    "> * *dtype*参数用于指定元素数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 0 0]\n",
      "[1 1 1 1 1]\n",
      "[3 3 3 3 3]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.zeros(size, dtype=np.int32)\n",
    "a2 = np.ones(size, dtype=np.int32)\n",
    "a3 = np.full(size, 3)\n",
    "print(a1)\n",
    "print(a2)\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">***list*与*array*的转换**  \n",
    ">* *Python*中，通过<code>a1 = [1,2,3,4,5]</code>创建一个*list*\n",
    ">* 虽然在很多情况下,*list*可以与*array*混用，但在某些情况下，numpy的函数只能针对array进行\n",
    ">* 通过下列代码将*list*(可以是一维，也可以是多维)转换成array：<code>a2 = np.array(a1)</code>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "(5,)\n"
     ]
    }
   ],
   "source": [
    "a1 = [1,2,3,4,5]\n",
    "#print(a1.shape)    # 错误，list对象没有shape属性\n",
    "print(len(a1))\n",
    "a2 = np.array(a1)\n",
    "print(a2.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **分别创建下列二维数组/矩阵：**  \n",
    "$ \\left[\n",
    "\\begin{matrix}\n",
    "1 & 2 & 3 \\\\\n",
    "4 & 5 & 6 \\\\\n",
    "\\end{matrix}\n",
    "\\right] $，\n",
    "$ \\left[\n",
    "\\begin{matrix}\n",
    "0 & 0 & 0 \\\\\n",
    "0 & 0 & 0 \\\\\n",
    "\\end{matrix}\n",
    "\\right] $，\n",
    "$ \\left[\n",
    "\\begin{matrix}\n",
    "1.0 & 1.0 & 1.0 \\\\\n",
    "1.0 & 1.0 & 1.0 \\\\\n",
    "\\end{matrix}\n",
    "\\right] $，\n",
    "$ \\left[\n",
    "\\begin{matrix}\n",
    "3 & 3 & 3 \\\\\n",
    "3 & 3 & 3 \\\\\n",
    "\\end{matrix}\n",
    "\\right] $  \n",
    ">* *zeros*, *ones* 和 *full* 方法需要提供数组每个维度的长度(即行和列的数量)，使用*(rows, cols)*的形式来表示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:\n",
      " [[1 2 3]\n",
      " [4 5 6]]\n",
      "a2:\n",
      " [[0. 0. 0.]\n",
      " [0. 0. 0.]]\n",
      "a3:\n",
      " [[1. 1. 1.]\n",
      " [1. 1. 1.]]\n",
      "a4:\n",
      " [[3 3 3]\n",
      " [3 3 3]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a1 = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "N = 2\n",
    "M = 3\n",
    "a2 = np.zeros((N, M))\n",
    "a3 = np.ones((N, M), dtype=np.float)\n",
    "a4 = np.full((N, M), 3)\n",
    "print(\"a1:\\n\", a1)\n",
    "print(\"a2:\\n\", a2)\n",
    "print(\"a3:\\n\", a3)\n",
    "print(\"a4:\\n\", a4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">**分别创建下列形式的二维数组/矩阵：**  \n",
    "$ \\left[\n",
    "\\begin{matrix}\n",
    "1, 0, 0 \\\\\n",
    "0, 1, 0 \\\\\n",
    "0, 0, 1 \\\\\n",
    "\\end{matrix}\n",
    "\\right]$，\n",
    "$ \\left[\n",
    "\\begin{matrix}\n",
    "1, 0, 0, 0 \\\\\n",
    "0, 1, 0, 0 \\\\\n",
    "0, 0, 1, 0 \\\\\n",
    "\\end{matrix}\n",
    "\\right]$  \n",
    ">* *identity*，*eye*均可创建单位方阵\n",
    ">* *eye*也可以创建非方阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:\n",
      " [[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n",
      "a2:\n",
      " [[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n",
      "a3:\n",
      " [[1. 0. 0. 0.]\n",
      " [0. 1. 0. 0.]\n",
      " [0. 0. 1. 0.]]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.identity(3)\n",
    "a2 = np.eye(3)\n",
    "a3 = np.eye(3,4)\n",
    "print(\"a1:\\n\", a1)\n",
    "print(\"a2:\\n\", a2)\n",
    "print(\"a3:\\n\", a3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **创建一个矩阵，但是不设置任何初始值**  \n",
    "> * *empty*创建一个未经初始化的矩阵，每个元素的值是不确定的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:\n",
      " [[2.31297541e-312 2.35541533e-312 2.46151512e-312 2.33419537e-312\n",
      "  2.31297541e-312]\n",
      " [2.56761491e-312 2.05833592e-312 6.79038654e-313 2.37663529e-312\n",
      "  4.94065646e-323]]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.empty((2, 5))\n",
    "print(\"a1:\\n\", a1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">**创建下列矩阵：**  \n",
    "$ \\left[\n",
    "\\begin{matrix}\n",
    "1 & 0 & 0 \\\\\n",
    "0 & 2 & 0 \\\\\n",
    "0 & 0 & 3 \\\\\n",
    "\\end{matrix}\n",
    "\\right] $，\n",
    "$ \\left[\n",
    "\\begin{matrix}\n",
    "0 & 1 & 0 & 0 \\\\\n",
    "0 & 0 & 2 & 0 \\\\\n",
    "0 & 0 & 0 & 3 \\\\\n",
    "0 & 0 & 0 & 0 \\\\\n",
    "\\end{matrix}\n",
    "\\right] $，\n",
    "$ \\left[\n",
    "\\begin{matrix}\n",
    "0 & 0 & 0 & 0 \\\\\n",
    "1 & 0 & 0 & 0 \\\\\n",
    "0 & 2 & 0 & 0 \\\\\n",
    "0 & 0 & 3 & 0 \\\\\n",
    "\\end{matrix}\n",
    "\\right] $\n",
    ">* *diag*用于创建对角矩阵(可以不是方阵)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:\n",
      " [[1 0 0]\n",
      " [0 2 0]\n",
      " [0 0 3]]\n",
      "a2:\n",
      " [[0 1 0 0]\n",
      " [0 0 2 0]\n",
      " [0 0 0 3]\n",
      " [0 0 0 0]]\n",
      "a3:\n",
      " [[0 0 0 0]\n",
      " [1 0 0 0]\n",
      " [0 2 0 0]\n",
      " [0 0 3 0]]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.diag([1,2,3])\n",
    "a2 = np.diag([1,2,3], k=1)\n",
    "a3 = np.diag([1,2,3], k=-1)\n",
    "print(\"a1:\\n\", a1)\n",
    "print(\"a2:\\n\", a2)\n",
    "print(\"a3:\\n\", a3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **已知某(M, N)矩阵，快速创建维度为(M, N)的全0矩阵，全1矩阵及元素值全为5的矩阵**  \n",
    ">* *zeros_like*，*ones_like*，*full_like*提供了基于某个矩阵模板快速创建矩阵的方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:\n",
      " [[0 0 0]\n",
      " [0 0 0]\n",
      " [0 0 0]]\n",
      "a2:\n",
      " [[1 1 1]\n",
      " [1 1 1]\n",
      " [1 1 1]]\n",
      "a3:\n",
      " [[5 5 5]\n",
      " [5 5 5]\n",
      " [5 5 5]]\n"
     ]
    }
   ],
   "source": [
    "a0 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "a1 = np.zeros_like(a0)\n",
    "a2 = np.ones_like(a0)\n",
    "a3 = np.full_like(a0, 5)\n",
    "print(\"a1:\\n\", a1)\n",
    "print(\"a2:\\n\", a2)\n",
    "print(\"a3:\\n\", a3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "### **案例2：使用区域分割方法生成序列**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **指定元素之间的间隔，生成下列序列(一维数组)：**  \n",
    "[0,1,2,3,4,5,6,7,8,9]  \n",
    "[1,2,3,4,5,6,7,8,9,10]  \n",
    "[1,3,5,7,9,11,13,15,17,19]  \n",
    "[0,0.2,0.4,0.6,0.8]  \n",
    ">* *arange* 提供了根据指定范围 *(min, max)* 和间隔 *(step)* 来生成序列的方法\n",
    ">* *arange* 中指定范围时，序列中将包含左侧边界值(最小值)，不包含右侧边界值(最大值),也即：*[min, max)*\n",
    ">* 可以指定浮点数间隔\n",
    ">* 请注意观察*arange*函数的默认参数设定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "[ 1  3  5  7  9 11 13 15 17 19]\n",
      "[0.  0.2 0.4 0.6 0.8]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.arange(10)\n",
    "a2 = np.arange(1, 10)\n",
    "a3 = np.arange(1, 20, 2)\n",
    "a4 = np.arange(0, 1.0, 0.2)\n",
    "print(a1)\n",
    "print(a2)\n",
    "print(a3)\n",
    "print(a4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **指定序列长度(数组元素个数)，生成序列**  \n",
    ">* *linspace* 方法通过指定范围*(min, max)*及数据点个数来生成序列\n",
    ">* 设定第4个参数为False时，生成的序列中将不包含右侧边界值，也即：*[min, max)*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.         0.33333333 0.66666667 1.         1.33333333 1.66666667\n",
      " 2.         2.33333333 2.66666667 3.        ]\n",
      "[0.  0.3 0.6 0.9 1.2 1.5 1.8 2.1 2.4 2.7]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.linspace(0, 3, 10)\n",
    "a2 = np.linspace(0, 3, 10, False)\n",
    "print(a1)\n",
    "print(a2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **创建下列形式的序列**：  \n",
    "[10, 100, 1000, 10000, 100000]  \n",
    "[2, 4, 8, 16, 32, 64, 128, 256]   \n",
    "[2, 8, 32, 128, 512]\n",
    ">* *logspace*方法以10为底，以指定的范围及间隔生成等距序列作为幂，然后执行指数操作，操作结果形成最终的序列\n",
    ">* *base*参数可以更改底"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[    10    100   1000  10000 100000]\n",
      "[  2   4   8  16  32  64 128 256]\n",
      "[  2   8  32 128 512]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.logspace(1, 5, 5, dtype=np.int)    # 幂序列值范围[1, 5]，共5个元素，因此就是[1,2,3,4,5]，然后以10为底计算幂次方结果\n",
    "a2 = np.logspace(1, 8, 8, base=2, dtype=np.int)    # 幂序列：[1,2,3,4,5,6,7,8]，然后以2为底计算幂次方结果\n",
    "a3 = np.logspace(1, 9, 5, base=2, dtype=np.int)    # 幂序列：[1,3,5,7,9]，然后以2为底计算幂结果\n",
    "print(a1)\n",
    "print(a2)\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **查看*logspace*产生的序列值分布**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAD8CAYAAABzTgP2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAADvlJREFUeJzt23+s3Xddx/Hny14YA+J+drC1q51ZHRaNTk8GiJqFsR9EoYvOWIzamJn+w5Qfim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+EvjnqnoF8FOs8uuTZB3w+0Cvqn4CWANsHe1UI/M3wFXz9l0P3FNVm4B7uu2hMwxDVFWHq+qB7vH/MPuPfN1opxoPSdYDvwTcOupZxkWSHwZ+EbgNoKqeraonRzvVWJgCTk0yBbwYODTieUaiqv4VODpv9xZgZ/d4J3D1SpzbMKyQJBuBi4H7RjvJ2PgQ8EfA90c9yBj5UeAI8NfdR2y3JnnJqIcapap6DPgg8A3gMPBUVf3LaKcaKy+rqsMw+x9R4JyVOIlhWAFJXgr8HfC2qvrvUc8zakl+GXi8qvaOepYxMwX8DPDRqroY+F9W6KOBSdF9Zr4FuAA4D3hJkt8c7VSrj2EYsiQvYDYKn6iqu0c9z5h4LfCmJF8D7gBel+RvRzvSWJgBZqrq+LvKu5gNxWr2euCrVXWkqr4L3A383IhnGiffTHIuQPf18ZU4iWEYoiRh9vPih6vqL0Y9z7ioqhuqan1VbWT2RuK9VbXq/xdYVf8FPJrkom7XZcCXRzjSOPgG8OokL+7+PV3GKr8hP88uYFv3eBvw6ZU4ydRKHHQVey3wW8BDSb7Y7fuTqvrHEc6k8fZ7wCeSvBB4BPidEc8zUlV1X5K7gAeY/Sm/B1mlvwGd5HbgUuDsJDPAjcDNwCeTXMtsRH9tRc7tbz5LkubyoyRJUsMwSJIahkGS1DAMkqSGYZAkNQyDJKlhGCRJDcMgSWr8P/EtGG/f5bErAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1a63195e908>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "N = 10\n",
    "x = np.logspace(0.1, 1, N, endpoint=True)\n",
    "y = np.zeros(N)\n",
    "plt.plot(x, y, 'o')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "### **案例3：数组的引用与拷贝**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **已知：**  \n",
    "a1 = np.array([1,2,3,4,5])  \n",
    "a2 = a1  \n",
    "a1[0] = 100  \n",
    "则此时a2[0]=?\n",
    ">* *=*运算符将使等号左右两边的数组变量指向同一块数据内存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    }
   ],
   "source": [
    "a1 = np.array([1,2,3,4,5])\n",
    "a2 = a1\n",
    "a1[0] = 100\n",
    "print(a2[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **已知：**  \n",
    "a1 = np.array([1,2,3,4,5])  \n",
    "np.copyto(a2, a1)  \n",
    "a3 = np.copy(a1)        \n",
    "a4 = np.array(a1)    \n",
    "则此时a2[0]=?，a3[0]=?，a4[0]=?\n",
    ">* *copyto*，*copy*，*array*函数都将产生一个新的数据块拷贝，这将导致即使修改了原始数组数据(a1)，也不会影响新数组中的数据(a2,a3,a4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "a1 = np.array([1,2,3,4,5])  \n",
    "a2 = np.empty(5, dtype=np.int)    # 给a2分配5个元素内存空间，但不作初始化\n",
    "np.copyto(a2, a1)  \n",
    "a3 = np.copy(a1)        \n",
    "a4 = np.array(a1) \n",
    "a1[0] = 100\n",
    "print(a2[0])\n",
    "print(a3[0])\n",
    "print(a4[0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
